package hzau.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hzau.constants.ResultConstant;
import hzau.dao.CollegeDao;
import hzau.dao.TeacherDao;
import hzau.pojo.College;
import hzau.pojo.Teacher;
import hzau.pojo.requestParams.CollegeRegisterParam;
import hzau.result.Result;
import hzau.service.CollegeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CollegeServiceImpl implements CollegeService {
    @Autowired
    CollegeDao collegeDao;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    TeacherDao teacherDao;


    final Integer PAGENUM=10;
    /*TODO
    * 增加两级缓存Redis和Caffeine
    *
    *
    * */
    @Override
    public Result register(CollegeRegisterParam college) {
        deleteFromRedis(college.getUniversityId());
        LambdaQueryWrapper<College> lqw=new LambdaQueryWrapper<>();
        lqw.eq(College::getName,college.getName());
        lqw.eq(College::getUniversityId,college.getUniversityId());
        College college1=collegeDao.selectOne(lqw);
        if (college1==null){
            collegeDao.insert(new College(college));
            return Result.success(null);
        }else
            return Result.error(ResultConstant.ALREADY_REGISTER_CODE,ResultConstant.COLLEGE_ALREADY_EXIST);
    }

    @Override
    public Result delete(Long id) {
        College college = collegeDao.selectById(id);
        if (college!=null){
            deleteFromRedis(college.getUniversityId());
            collegeDao.deleteById(id);
            return Result.success(null);
        }else return Result.error(ResultConstant.ERROR_CODE,ResultConstant.NO_SUCH_COLLEGE);
    }

    @Override
    public Result getOne(Long universityId, Long collegeId) {
        String key="university_"+universityId.toString();
        List<College> collegeList;
        //先在Redis中找信息
        collegeList = (List<College>)redisTemplate.opsForValue().get(key);
        //redis中有对应学院的教师列表
        if (collegeList!=null){
            for (College college : collegeList) {
                if (college.getId().equals(collegeId)){
                    //找到信息返回
                    return Result.success(college);
                }
            }
            //没找到该学院
            return Result.error(ResultConstant.NOT_EXIST,ResultConstant.NO_SUCH_COLLEGE);
        }
        College college = collegeDao.selectById(collegeId);
        if (college!=null&&college.getUniversityId().equals(universityId)){
            updateRedis(universityId);
            return Result.success(college);
        }else
            return Result.error(ResultConstant.NOT_EXIST,ResultConstant.NO_SUCH_COLLEGE);
    }

    @Override
    public Result getAll(Long universityId, Integer page) {
//        String key="university_"+universityId.toString();
//        List<College> collegeList = (List<College>) redisTemplate.opsForValue().get(key);
//        if (collegeList!=null){
//            log.info("从缓存中找信息");
//            return Result.success(collegeList);
//        }//从数据库中找
//        else {
//            log.info("从数据库中找信息");
            LambdaQueryWrapper<College> lqw=new LambdaQueryWrapper<>();
            lqw.eq(College::getUniversityId,universityId);
            IPage<College> collegePage=new Page<>(page,PAGENUM);
            IPage<College> collegeList = collegeDao.selectPage(collegePage,lqw);
            //TODO 实现缓存穿透
            if (collegeList!=null){
                return Result.success(collegeList);
            }else return Result.error(ResultConstant.NOT_EXIST,ResultConstant.NO_SUCH_COLLEGE);
//        }
    }
    @Override
    public Result update(College college) {
        deleteFromRedis(college.getUniversityId());
        LambdaQueryWrapper<College> lqw =new LambdaQueryWrapper<>();
        lqw.eq(College::getId, college.getId());
        College college1 = collegeDao.selectById(college.getId());
        if(college1!=null){
        collegeDao.update(college,lqw);
            return Result.success(null);
        }else {
            return Result.error(ResultConstant.NOT_EXIST,ResultConstant.NO_SUCH_COLLEGE);
        }

    }

    @Override
    public Result getAllCollege(Long universityId) {
        LambdaQueryWrapper<College> lqw=new LambdaQueryWrapper<>();
        lqw.eq(College::getUniversityId,universityId);
        List<College> collegeList = collegeDao.selectList(lqw);
        return Result.success(collegeList);
    }

    @Override
    public Result getPresident(Long collegeId) {
        College college = collegeDao.selectById(collegeId);
        Teacher teacher = teacherDao.selectById(college.getPresidentId());
        return Result.success(teacher);
    }

    @Override
    public Result setPresident(Long collegeId, Long id) {
        College college = collegeDao.selectById(collegeId);
        college.setPresidentId(id);
        collegeDao.updateById(college);
        return Result.success(null);
    }

    /*更新Redis中该学校学院的信息*/
    @Override
    public void updateRedis(Long universityId) {
        String key="university_"+universityId.toString();
        LambdaQueryWrapper<College> lqw=new LambdaQueryWrapper<>();
        lqw.eq(College::getUniversityId,universityId);
        List<College> teacherList = collegeDao.selectList(lqw);
        redisTemplate.opsForValue().set(key,teacherList);
    }
    /*删除Redis中该学院教师的信息*/
    @Override
    public void deleteFromRedis(Long universityId) {

        String key="university_"+universityId.toString();
        redisTemplate.delete(key);
    }


}
